	var http = null;

	var cityChecked = 0;
	
if (document.layers) { // Netscape
    document.captureEvents(Event.MOUSEMOVE);
    document.onmousemove = captureMousePosition;
} else if (document.all) { // Internet Explorer
    document.onmousemove = captureMousePosition;
} else if (document.getElementById) { // Netcsape 6
    document.onmousemove = captureMousePosition;
}

// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen
xMousePosMax = 0; // Width of the page
yMousePosMax = 0; // Height of the page

function captureMousePosition(e) {
    if (document.layers) {
        // When the page scrolls in Netscape, the event's mouse position
        // reflects the absolute position on the screen. innerHight/Width
        // is the position from the top/left of the screen that the user is
        // looking at. pageX/YOffset is the amount that the user has
        // scrolled into the page. So the values will be in relation to
        // each other as the total offsets into the page, no matter if
        // the user has scrolled or not.
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {
        // When the page scrolls in IE, the event's mouse position
        // reflects the position from the top/left of the screen the
        // user is looking at. scrollLeft/Top is the amount the user
        // has scrolled into the page. clientWidth/Height is the height/
        // width of the current page the user is looking at. So, to be
        // consistent with Netscape (above), add the scroll offsets to
        // both so we end up with an absolute value on the page, no
        // matter if the user has scrolled or not.
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        // Netscape 6 behaves the same as Netscape 4 in this regard
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }

//window.status = "xMousePos=" + xMousePos + ", yMousePos=" + yMousePos + ", xMousePosMax=" + xMousePosMax + ", yMousePosMax=" + yMousePosMax;

}

function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

   function getHttpObj() {
      http = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http = new XMLHttpRequest();
         if (http.overrideMimeType) {
            http.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
   }

   function handleResponse() {
      if (http.readyState == 4) {
         if (http.status == 200) {
				var result = http.responseText;
//            el = document.getElementById('2breplaced');
//				el.innerHTML = result;            
//				alert( "result=" +result );

				var xmlDocument = http.responseXML;
				//var street = xmlDocument.getElementsByTagName('address').item(0).firstChild.data;
				var street = xmlDocument.getElementsByTagName('address');

				document.getElementById('choosestreet').style.top	= yMousePos + 40;
				document.getElementById('choosestreet').style.left	= xMousePos - 200;
				document.getElementById('choosestreet').style.visibility = "visible";

				var j = 0;
				document.forms['VENUEEDIT'].streetname.options.length = 0;
				document.forms['VENUEEDIT'].streetname.options[j] = new Option('Seleziona dalla lista...','');
				for( var i = 0; street[i] && i < 500; i++ ) {
								document.getElementById('city').value = xmlDocument.getElementsByTagName('geoinfo')[0].attributes.getNamedItem("descr").value;
								document.forms['VENUEEDIT'].streetname.options[++j] = new Option(	
										TrimString(street[i].attributes.getNamedItem("streetname").value) + ' '+
										TrimString(street[i].attributes.getNamedItem("streetprefix").value),
										street[i].attributes.getNamedItem("streetid").value);
				//				alert( "result=" +street[i].attributes.getNamedItem("streetname").value );
				}
				document.forms['VENUEEDIT'].streetname.options[j] = new Option('Inserisci manualmente...','');
				document.getElementById('viamichelincountry').value = xmlDocument.getElementsByTagName('geoinfo')[0].attributes.getNamedItem("michelin").value;

         } else {
            alert('There was a problem with the request.');
			var result = http.responseText;
			alert( result );
         }
      }
   }

   function postZip() {
		getHttpObj();
      if (!http) {
         alert('XMLHTTP instance not created!');
         return false;
      }

      var parameters = "ajax=true" 
					+	"&zip=" + document.getElementById("zip").value
					+  "&country=" + document.getElementById("country").value;
      var url = '/ajax/venues.aspx';
      
      http.onreadystatechange = handleResponse;
      http.open('POST', url, true);
      http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http.setRequestHeader("Content-length", parameters.length);
      http.setRequestHeader("Connection", "close");
      http.send(parameters);
   }

   function streetSelected() {
		var i = document.forms['VENUEEDIT'].streetname.selectedIndex;
//		alert( "selected=" + document.forms['VENUEEDIT'].streetname.options[i].value );
		document.getElementById('address').value = document.forms['VENUEEDIT'].streetname.options[i].text;
		document.getElementById('address').style.textTransform = 'capitalize'
		document.getElementById('choosestreet').style.visibility = "hidden";

   }

   function testViamichelin() {
		var address = document.getElementById('address').value+'+'+document.getElementById('nr').value;
		address = address.replace(' ','+');
		address = address.replace(' ','+');
		address = address.replace(' ','+');
		address = address.replace(' ','+');
		address = address.replace(' ','+');
		address = address.replace('(','');
		address = address.replace(')','');
		if( document.getElementById('viamichelin') != null ) {
			document.getElementById('viamichelin').target = '_blank';
			document.getElementById('viamichelin').href = 'http://www.viamichelin.com/viamichelin/ita/dyn/controller/mapPerformPage?strAddress='
					+address
					+'&strCP='
					+document.getElementById('zip').value
					+'&strLocation='
					+document.getElementById('city').value
					+'&strCountry='
					+document.getElementById('viamichelincountry').value				
					+'&image2.x=0&image2.y=0'
		}
   }

   function testGooglemapFullScreen() {
		document.getElementById('testGooglemapFullScreen').target = '_blank';
		document.getElementById('testGooglemapFullScreen').href = '/gmap.aspx?'
				+"longitude=" +document.getElementById('longitude').value
				+'&latitude=' +document.getElementById('latitude').value
				+'&name=' +document.getElementById('activity.TITLE').value
				+'&address=' +document.getElementById('address').value
				+'&nr=' +document.getElementById('nr').value
				+'&zip=' +document.getElementById('zip').value
				+'&city=' +document.getElementById('city').value
				+'&country=' +document.getElementById('country').value ;
   }
   
   function testGooglemap() {
		document.getElementById('gmapcontainer').style.visibility = "visible";
		document.getElementById('gmapcontainer').style.top	= yMousePos - 80;
		document.getElementById('gmapcontainer').style.left	= xMousePos - 200;

		var lon = document.getElementById('longitude').value;
		var lat = document.getElementById('latitude').value;

		// Center the map on Palo Alto.
		var map = new GMap(document.getElementById("googlemap"));
		map.setMapType(map.mapTypes[1]);
		map.addControl(new GSmallMapControl());
		map.centerAndZoom(new GPoint(lon, lat), 1);

//		map.openInfoWindow(map.getCenterLatLng(),document.createTextNode("Hello, world"));

		// Add 10 random markers in the map viewport using the default icon.
		var point = new GPoint(lon, lat);
		var marker = new GMarker(point);
		map.addOverlay(marker);

   }

   function preloadGooglemap() {
		var lon = document.getElementById('longitude').value;
		var lat = document.getElementById('latitude').value;

		// Center the map on Palo Alto.
		var map = new GMap(document.getElementById("googlemap"));
		map.setMapType(map.mapTypes[1]);
		map.addControl(new GSmallMapControl());
		map.centerAndZoom(new GPoint(lon, lat), 1);

//		map.openInfoWindow(map.getCenterLatLng(),document.createTextNode("Hello, world"));

		// Add 10 random markers in the map viewport using the default icon.
		var point = new GPoint(lon, lat);
		var marker = new GMarker(point);
		map.addOverlay(marker);

   }

	function validateVenue() {
		document.returnValue = true;
		var keys = TrimString(document.getElementById('keywords').value);
		if( keys.length > 0 ) {
			var ak = keys.split(",");
			if( ak.length < 3 ) {
				alert("Devi inserire almeno 3 keyword!");
				document.returnValue = false;
			}
		}else{
			alert("Devi inserire almeno 3 keyword!");
			document.returnValue = false;
		}
	}   

	function validateActivity() {
		var submit;

		document.returnValue = true;
	// check title
		var TITLE = TrimString(document.getElementById('activity.TITLE').value);
		if( TITLE.length <= 0 ) {
			alert("Devi inserire un titolo per l'evento!");
			document.returnValue = false;
			return;
		}
	// check descr
	//	it's handled server side due to possible use of tinymce
	//	the eventuality user create an event and forget to put a descriptio is quite unsual
	// check keywords
		var keys = TrimString(document.getElementById('keywords').value);
		if( keys.length > 0 ) {
			var ak = keys.split(",");
			if( ak.length < 3 ) {
				alert("Devi inserire almeno 3 keyword!");
				document.returnValue = false;
				return;
			}
		}else{
			alert("Devi inserire almeno 3 keyword!");
			document.returnValue = false;
			return;
		}
		if( document.getElementById('LID') == null || document.getElementById('LID').value <= 0 ) {
	// check address
			var address = TrimString(document.getElementById('address').value);
			if( address.length <= 0 ) {
				alert("Devi inserire un indirizzo per l'evento!");
				document.returnValue = false;
				return;
			}
		// check nr
			var nr = TrimString(document.getElementById('nr').value);
			if( nr.length <= 0 ) {
				alert("Devi inserire un numero civico per l'indirizzo dell'evento!");
				document.returnValue = false;
				return;
			}
		// check zip
			var zip = TrimString(document.getElementById('zip').value);
			if( zip.length <= 0 ) {
				alert("Devi inserire un cap per l'indirizzo dell'evento!");
				document.returnValue = false;
				return;
			}
		// check city
			var city = TrimString(document.getElementById('city').value);
			if( city.length <= 0 ) {
				alert("Devi inserire una citta' per l'indirizzo dell'evento!");
				document.returnValue = false;
				return;
/*
			}else{
				if( cityChecked == 0 )
					ajaxCheckCityAndZip(1);
				if( cityChecked != 1 ){
					alert("La citta' che hai inserito non risulta esistente!");
					document.returnValue = false;
					return;
				}
*/
			}
		}   
	// check date
		var DUEDATE = TrimString(document.getElementById('DUEDATE').value);
		if( DUEDATE.length <= 0 ) {
			alert("Devi inserire una data dell'evento!");
			document.returnValue = false;
			return;
		}
		if( !isDate(DUEDATE,"y-M-d") ) {
			alert("Devi inserire una data CORRETTA!");
			document.returnValue = false;
			return;
		}
	// check hour
		var DUEHH = TrimString(document.getElementById('DUE.HH').value);
		if( DUEHH.length <= 0 ) {
			alert("Devi inserire un orario dell'evento!");
			document.returnValue = false;
			return;
		}
	// check minutes
		var DUEMI = TrimString(document.getElementById('DUE.MI').value);
		if( DUEMI.length <= 0 ) {
			alert("Devi inserire ora e MINUTI dell'evento!");
			document.returnValue = false;
			return;
		}

		if( document.getElementById('SUBMIT1') )
			submit = document.getElementById('SUBMIT1');
		if( document.getElementById('SUBMIT2') )
			submit = document.getElementById('SUBMIT2');
		submit.value = "wait...";
		submit.disabled = true;
	}   

function handleCheckCityAndZip() {
	document.getElementById('waiting').style.visibility = "hidden";
    if (http.readyState == 4) {
        if (http.status == 200) {
			var result = http.responseText;
			if( result == 1 ) {
				cityChecked = 1;			
			}
			//alert( "result=" +result );
        } else {
	        alert('There was a problem with the request.');
        }
    }
}

function ajaxCheckCityAndZip(sync) {
	document.getElementById('waiting').style.visibility = "visible";
	getHttpObj();
	if (!http) {
		alert('XMLHTTP instance not created!');
		return false;
	}

	var parameters = "ajax=true" 
				+	"&zip=" + document.getElementById("zip").value
				+	"&city=" + document.getElementById("city").value
				+  "&country=" + document.getElementById("country").value;
	var url = '/ajax/geo.aspx';

	if( sync == 1 ){
		http.onload = handleCheckCityAndZip;		
		http.open('POST', url, false);
	}else{
		http.onreadystatechange = handleCheckCityAndZip;
		http.open('POST', url, true);
	}
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", parameters.length);
	http.setRequestHeader("Connection", "close");
	http.send(parameters);
}

